using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace Emlyn.Applications.LochAlarm
{
    [XmlRoot("DailySettings")]
    public class DailySettings: IXmlSerializable
    {
        private bool _enabled;

        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }

        private TimeSpan _alarmTime;

        public TimeSpan AlarmTime
        {
            get { return _alarmTime; }
            set { _alarmTime = value; }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.ReadStartElement();

            while (reader.LocalName != "DailySettings")
            {
                if (reader.LocalName == "Enabled")
                {
                    reader.ReadStartElement();
                    _enabled = reader.ReadContentAsBoolean();
                    reader.ReadEndElement();
                }
                else if (reader.LocalName == "AlarmTime")
                {
                    reader.ReadStartElement();
                    _alarmTime = new TimeSpan(reader.ReadContentAsLong());
                    reader.ReadEndElement();
                }
            }

            reader.ReadEndElement();
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteStartElement("Enabled");
            writer.WriteValue(_enabled);
            writer.WriteEndElement();

            writer.WriteStartElement("AlarmTime");
            writer.WriteValue(_alarmTime.Ticks);
            writer.WriteEndElement();
        }

        #endregion
    }
}
